<?php
	require ('db_conn.php');
	function display_login() {
		?>
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
			<title>Student grades manage sysytem</title>
			</head>
			<body>
			<form name="formlogin" method="post" action="menu.php" value=" ">
			<div style="height:500px; width:500px; margin:0 auto;">
			<div class="div_subimt">
			<span>Username:</span>
			<span><input type="text" name="username" class="username" /></span>
			<span><input type="submit" name="submit" class="submit" value="Submit" /></span>
			</div>
			<div class="div_reset">
			<span>Password:</span>
			<span><input type="password" name="password" class="password" /></span>
			<span><input type="reset" name="reset" class="reset" value="Reset" /></span>
			<a href="register.php" target=_blank>Register</a>
			</div>
			</div> 
			</form>
			</body>
			</html>
		<?php
	}
	function display_register(){
		?>
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
			<title>Student grades manage sysytem</title>
			</head>
			<body>
			<form name="formlogin" method="post" action="register.php" value=" ">
			<div style="height:500px; width:500px; margin:0 auto;">
			<div class="div_subimt">
			<span>Username:</span>
			<span><input type="text" name="username" class="username" /></span>
			<span><input type="submit" name="submit" class="submit" value="Regist" /></span>
			</div>
			<div class="div_reset">
			<span>Password:</span>
			<span><input type="password" name="password" class="password" /></span>
			<span><input type="reset" name="reset" class="reset" value="Reset" /></span>
			</div>
			</div> 
			</form>
			</body>
			</html>
		<?php
	}
	function display_dataget() {
		$conn = db_connect();
		$result = $conn->query("select * from grades where username = '".$_SESSION['valid_user']."'");
		if (!$result){
			return false;
		}
		return $result;
	}
	function display_datashow($tempdata){
		echo "<p>Following are your grades";
		$row = $tempdata->fetch_assoc();
		echo "<p>Math: ";
		echo stripslashes($row['math']);
		echo "<br />Computer: ";
		echo stripslashes($row['computer']);
		echo "<br />P.E: ";
		echo stripslashes($row['pe']);
		echo "</p>";
	}
		
	function display_menu(){
		?>
			<table border=0>
			<tr>
			<td><a href="show.php" target=_blank>Show</a></td>
			<td><a href="blog_edit.php" target=_blank>Blog-edit</a></td>
			<td><a href="insert.php" target=_blank>Insert</a></td>
			<td><a href="logout.php" target=_blank>Logout</a></td>
			</tr>
			</table>
		<?php
	}
	function blog_dataget(){
		$conn = db_connect();
		$result = $conn->query("select * from context where username = 'denghongcai'");
		if (!$result){
			return false;
		}
		return $result;
	}
	function display_blog($blogdata){
		$res_array=array();
		for ($count=0;$row=$blogdata->fetch_assoc();$count++){
			$res_array[$count]=$row;
		}
		$res_array=array_reverse($res_array);
		for ($count=0;$count<=4;$count++){
			$row=$res_array[$count];
			echo "<p>";
			echo stripslashes($row['title']);
			echo "<br /><br/>";
			echo stripslashes($row['text']);
			echo "</p>";
		}
	}
	function display_bloglist($blogdata){
		$res_array=array();
		for ($count=0;$row=$blogdata->fetch_assoc();$count++){
			$res_array[$count]=$row;
		}
		$res_array=array_reverse($res_array);
		foreach($res_array as $row){
			echo "<p>";
			echo stripslashes($row['title']);
			echo "<br />";
			echo "<a href=blog_modify.php?action=edit&title=".($row['title']).">edit</a>";
			echo "</p>";
		}
	}
	function blog_editdata($title){
		$conn = db_connect();
		$result = $conn->query("select * from context where username = '".$_SESSION['valid_user']."' and title ='".$title."'");
		$result = $result->fetch_assoc();
		$result=stripslashes($result['text']);
		if (!$result){
			return false;
		}
		return $result;
	}
	function blog_modify($content,$title){
		$conn = db_connect();
		$result = $conn->query("update context set text ='".$content."' where title='".$title."'");
		if (!$result){
			return false;
		}
	}
	function user_login($username,$password) {
		$conn = db_connect();
		$password = sha1('".$password."');
		$result = $conn->query ("select * from users where username = '".$username."' and password = '".$password."'");
		if (!$result){
			throw new Exception ('Failed to login in');
			}
		if($result->num_rows>0){
			return true;
			}
		else{
			throw new Exception ('Failed to login in');
		}
	}
	function check_valid_user(){
		if  ( isset ($_SESSION['valid_user'])){
			echo "Logged in as ".$_SESSION['valid_user'].".<br />";
			}
		else {
			echo 'Sorry,your login has failed.Maybe you can try again?';
			display_login();
			exit;
		}
	}
	function user_register($username,$password){
		$conn = db_connect();
		$password = sha1('".$password."');
		$result = $conn->query( "insert into `users` (`username`,`password`) values ( '".$username."',  '".$password."')");
		if (!$result){
			throw new Exception ('Failed to login in');
			}
		$result = $conn->query("insert into `grades` (`username`) values ('".$username."')");
		return true;
	}
?>